import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Window 2.12
import QtLocation 5.12
import QtQuick.Dialogs 1.3
import TableModel 0.1
import Qt.labs.qmlmodels 1.0
import QtQuick.Layouts 1.12
import AIClient.module 1.0
Page {
    id: model_page
    width: parent.width*0.8
    height: parent.height*0.8

    title: qsTr("模型")
    anchors.fill: parent
//    ListModel {
//        id: model
//        ListElement { no: "red"; }
//    }
//    property string m_token: ""
//    function setToken(token) {
//        m_token = token
//    }
//    AIClient{
//        id:client
//    }

//    ModelCreate {
//        id:model_create
//        onSuccess: {
//            tip.showmsg("创建模型成功")
//            updateModel()
//        }

//    }

//    TipMessage {
//        id: tip
//        width: parent.parent.width
//    }

    property int m_mode: 0
    signal delModel(string uid)
    signal createModel()
    signal editModel(string uid)
    signal grantModel(string uid, string model_name)
    signal manageModelUser(string uid, string model_name, int role)
    signal viewModel(string uid)
    signal updateContent()
    signal successUpdateModels()

    MessageDialog {
        id: messageDialog
        icon: StandardIcon.Question
        modality: Qt.ApplicationModal
        standardButtons: StandardButton.No | StandardButton.Yes
        title: "删除模型"
        text: "是否删除模型?"
        property string model_id: ""
        onAccepted: {
            console.log("And of course you could only agree.")
        }
        onYes: {
            delModel(model_id)
//            client.setConfig(m_token)
//            var res = client.delete_model(model_id)
//            var jres = JSON.parse(res)
//            if ( jres["code"] === 0 )
//            {
//                success_delete()
//            }
//            else {
//                failed_delete()
//            }

            //tip.showmsg("删除模型成功")
        }

//        Component.onCompleted: visible = true
    }


    //onUpdateModels: updateModel()
//    function updateModel() {
//        removeAll()
//        client.setConfig(m_token)
//        var res = client.list_model()
//        var jres = JSON.parse(res)
//        if ( jres["code"] !== 0 )
//        {
//            tip.showmsg(jres["message"])
//            return
//        }
//        tip.showmsg("获取模型成功")
//        var data = jres["data"]
//        for ( var i=0; i<data.length ; i++ )
//        {
//            data[i]["uid"] = data[i]["id"]
//            data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
//            addItem(jres["data"][i])
//        }
//    }

    function addItem(item) {
        myModel.append(item)
    }

    function removeAll() {
        myModel.clear()
    }

    function removeItem(item) {
        myModel.remove(item)
    }

    ListModel {
        /* 数据项 */
        id: myModel
        ListElement {
            uid: "uuid";
            name: "red";
            model_class: "test";
            dependencies: "denpend";
            create_date: "2020";
            role_text: "admin";
            role: 0;
            creator: "email";
        }
    }

    ListView {
        id:listView
        model: myModel
        anchors.centerIn: parent
//        width: parent.width*0.8
//        height: parent.height*0.8

//        anchors.bottomMargin: 100
//        anchors.leftMargin: 100
//        anchors.topMargin: 100
//        anchors.rightMargin: 100
        width: model_page.width*0.8
        height: model_page.height*0.8
        header:
            RowLayout {


            spacing: 0
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: 5
                Layout.preferredWidth: 5
                Layout.preferredHeight: 80
//                height: 80
//                width: 120
                color: "azure"
                Text {
                    anchors.centerIn: parent
                    //width: 120

                    text: "id"
                    visible: false
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.1
                Layout.preferredWidth: listView.width*0.1
                Layout.preferredHeight: 80
//                height: 80
//                width: 180
                color: "azure"
                Text {
                    text: "名称"
//                    width: 180

                    anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.1
                Layout.preferredWidth: listView.width*0.1
                Layout.preferredHeight: 80
//                height: 80
//                width: 180
                color: "azure"
                Text {
                    text: "模型类名"
//                    width: 180

                    anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }

            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.2
                Layout.preferredWidth: listView.width*0.2
                Layout.preferredHeight: 80
//                height: 80
//                width: 180
                color: "azure"
                Text {
                    text: "依赖"
//                    width: 180

                    anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.16
                Layout.preferredWidth: listView.width*0.16
                Layout.preferredHeight: 80
//                height: 80
//                width: 200
                color: "azure"
                Text {
                    text: "创建日期"
//                    width: 200
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.08
                Layout.preferredWidth: listView.width*0.08
                Layout.preferredHeight: 80
//                height: 80
//                width: 200
                color: "azure"
                Text {
                    text: "角色"
//                    width: 200
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.16
                Layout.preferredWidth: listView.width*0.16
                Layout.preferredHeight: 80
//                height: 80
//                width: 200
                color: "azure"
                Text {
                    text: "创建者"
//                    width: 200
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.1
                Layout.preferredWidth: listView.width*0.1
                Layout.preferredHeight: 80
//                height: 80
//                width: 160
                color: "azure"
                Text {
                    text: "操作"
//                    width: 180
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }

            Rectangle{
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.1
                Layout.preferredWidth: listView.width*0.1
                Layout.preferredHeight: 80
                color: "azure"
//                    height: 80
//                    width: 80
                OrangeButton {
                    visible: m_mode != 2
                    anchors.centerIn: parent
                    text: "创建模型"
                    onClicked: {
                        createModel(m_mode)
                        return
                    }
                }
            }

//            Button {
//                anchors.centerIn: parent

//                //anchors.centerIn: parent
//                text: "创建模型"
//                font.pixelSize: Qt.application.font.pixelSize*1.2
//                background: Rectangle {
//                    anchors.fill: parent
//                    color: "gold"
//                    border.color: "black"
//                    border.width: 2
//                    radius: 5
//                }
//                hoverEnabled: true


////                icon.source:  "/images/images/add.jpeg"
////                icon.height: 80
////                icon.width: 80
////                icon.color: transparent
////                flat: true
//                onClicked: {
//                    createModel(m_mode)
//                    return
////                    model_create.m_mode = 1
////                    model_create.title = "创建模型"
////                    model_create.m_content = ""
////                    model_create.m_dependencies = ""
////                    model_create.m_description = ""
////                    model_create.m_uid = ""
////                    model_create.m_model_name = ""
////                    model_create.m_model_class = ""
////                    model_create.setToken(m_token)
////                    model_create.show()
////                    update()


//                    //console.log("delete:", uid)
//                }
//            }
            }
            //}

        //}

        //topMargin: 50
        //bottomMargin: 50
//        width: 500
//        height: 600

        delegate: ItemDelegate {
            height: 80
            width: parent.width
            RowLayout {


                spacing: 0
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: 5
                    Layout.preferredWidth: 5
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 120
                    Text {
                        anchors.centerIn: parent
//                        width: 120
                        text: uid
                        visible: false
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.1
                    Layout.preferredWidth: listView.width*0.1
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: name
//                        width: 180
                        anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.1
                    Layout.preferredWidth: listView.width*0.1
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: model_class
//                        width: 180
                        anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }

                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.2
                    Layout.preferredWidth: listView.width*0.2
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: dependencies
//                        width: 180
                        anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.16
                    Layout.preferredWidth: listView.width*0.16
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: create_date
//                        width: 180
                        anchors.centerIn: parent
                                    //anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.08
                    Layout.preferredWidth: listView.width*0.08
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: role_text
//                        width: 180
                        anchors.centerIn: parent
                                    //anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.16
                    Layout.preferredWidth: listView.width*0.16
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: creator
//                        width: 180
                        anchors.centerIn: parent
                                    //anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                //}
                Rectangle{
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.02
                    Layout.preferredWidth: listView.width*0.02
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 80
                    Button {
                        width: parent.width
                        height: parent.width
                        anchors.centerIn: parent
                        ToolTip.delay: 1000
                        ToolTip.timeout: 5000
                        ToolTip.visible: hovered
                        ToolTip.text: qsTr("删除")
                    //anchors.centerIn: parent
                    //text: "delete"
                        background: Image {
                            source: {
                                if ( role === 0 )
                                {
                                    return "/images/images/delete.jpg"
                                }
                            }
                            anchors.centerIn: parent
                            width: parent.width*0.9
                            height: parent.width*0.9
                            opacity: parent.hovered?0.3:1
                        }

//                    icon.source:  "/images/images/delete.jpg"
//                    icon.height: parent.height*0.7
//                    icon.width: parent.height*0.7
//                    icon.color: transparent
                        flat: true
                        onClicked: {
                            messageDialog.model_id = uid
                            messageDialog.open()
                        //console.log("delete:", uid)
                        }
                        enabled: role === 0
                    }
                }
                Rectangle{
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.02
                    Layout.preferredWidth: listView.width*0.02
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 80
                    Button {
                        width: parent.width
                        height: parent.width
                        anchors.centerIn: parent
                        ToolTip.delay: 1000
                        ToolTip.timeout: 5000
                        ToolTip.visible: hovered
                        ToolTip.text: qsTr("查看")
                        background: Image {
                            source: {
                                if ( role <= 3 )
                                {
                                    return "/images/images/view.jpeg"
                                }
                            }
                            anchors.centerIn: parent
                            width: parent.width*0.9
                            height: parent.width*0.9
                            opacity: parent.hovered?0.3:1
                        }
                        enabled: role <= 3
                    //anchors.centerIn: parent
                    //text: "delete"
//                    icon.source:  "/images/images/edit.jpeg"
////                    icon.height: parent.height*0.7
////                    icon.width: parent.height*0.7
//                    icon.color: transparent
                        flat: true
                        onClicked: {
                            viewModel(uid)

                            return
//                            client.setConfig(m_token)
//                            var res = client.get_model(uid)
//                            var jres = JSON.parse(res)
//                            if ( jres["code"] === 0 ) {
//                                model_create.title = "查看模型"
//                                model_create.m_mode = 0
//                                model_create.m_content = jres["data"]["model_file_bytes"]
//                                model_create.m_dependencies = jres["data"]["dependencies"]
//                                model_create.m_description = jres["data"]["description"]
//                                model_create.m_uid = uid
//                                model_create.m_model_name = jres["data"]["name"]
//                                model_create.m_model_class = jres["data"]["model_class"]
//                                model_create.setToken(m_token)
//                                model_create.show()
//                                return
//                            }
//                            tip.showmsg(jres["message"])
                        }
                    }
                }
                Rectangle{
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.02
                    Layout.preferredWidth: listView.width*0.02
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 80
                    Button {
                        width: parent.width
                        height: parent.width
                        anchors.centerIn: parent
                        ToolTip.delay: 1000
                        ToolTip.timeout: 5000
                        ToolTip.visible: hovered
                        ToolTip.text: qsTr("编辑")
                        background: Image {
                            source: {
                                if ( role <= 2 )
                                {
                                    return "/images/images/edit.jpg"
                                }
                            }
                            anchors.centerIn: parent
                            width: parent.width*0.9
                            height: parent.width*0.9
                            opacity: parent.hovered?0.3:1
                        }
                        enabled: role <= 2
                    //anchors.centerIn: parent
                    //text: "delete"
//                    icon.source:  "/images/images/edit.jpeg"
////                    icon.height: parent.height*0.7
////                    icon.width: parent.height*0.7
//                    icon.color: transparent
                        flat: true
                        onClicked: {
                            editModel(uid)

                            return
//                            client.setConfig(m_token)
//                            var res = client.get_model(uid)
//                            var jres = JSON.parse(res)
//                            if ( jres["code"] === 0 ) {
//                                model_create.title = "查看模型"
//                                model_create.m_mode = 0
//                                model_create.m_content = jres["data"]["model_file_bytes"]
//                                model_create.m_dependencies = jres["data"]["dependencies"]
//                                model_create.m_description = jres["data"]["description"]
//                                model_create.m_uid = uid
//                                model_create.m_model_name = jres["data"]["name"]
//                                model_create.m_model_class = jres["data"]["model_class"]
//                                model_create.setToken(m_token)
//                                model_create.show()
//                                return
//                            }
//                            tip.showmsg(jres["message"])
                        }
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.02
                    Layout.preferredWidth: listView.width*0.02
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 80
                    Button {
                        width: parent.width
                        height: parent.width
                        anchors.centerIn: parent
                        ToolTip.delay: 1000
                        ToolTip.timeout: 5000
                        ToolTip.visible: hovered
                        ToolTip.text: qsTr("授权")
                    //anchors.centerIn: parent
                    //text: "delete"
                        background: Image {
                            source: {
                                if (role <= 1)
                                {
                                    return "/images/images/user.jpeg"
                                }
                            }
                            anchors.centerIn: parent
                            opacity: parent.hovered?0.3:1
                            width: parent.width*0.9
                            height: parent.width*0.9
                        }
                        flat: true
                        onClicked: {
                            grantModel(uid, name)
                        }
                        enabled: role <= 1
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.02
                    Layout.preferredWidth: listView.width*0.02
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 80
                    Button {
                        width: parent.width
                        height: parent.width
                        anchors.centerIn: parent
                        ToolTip.delay: 1000
                        ToolTip.timeout: 5000
                        ToolTip.visible: hovered
                        ToolTip.text: qsTr("管理成员")
                    //anchors.centerIn: parent
                    //text: "delete"
                        background: Image {
                            source: {
                                if (role <= 1)
                                {
                                    return "/images/images/usermanage.jpg"
                                }
                            }
                            anchors.centerIn: parent
                            width: parent.width*0.9
                            height: parent.width*0.9
                            opacity: parent.hovered?0.3:1
                        }
                        flat: true
                        onClicked: {
                            manageModelUser(uid, name, role)
                        }
                        enabled: role <= 1
                    }
                }
                Rectangle{
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.1
                    Layout.preferredWidth: listView.width*0.1
                    Layout.preferredHeight: 80
                    color: "lightyellow"
                }
//                MouseArea {
//                    hoverEnabled: true
//                    anchors.fill: parent
//                    onEntered: {

//                    }
//                    onExited: {

//                    }
//                }
            }
            Rectangle {
                anchors.bottom: parent.bottom
                height: 1
                width: parent.width
                color: "black"
            }
            //spacing: 30
            highlighted: false
            hoverEnabled: true
            onClicked:{
                        //listView.currentIndex = index
                console.log("clicked:", uid)
            }
        }
    }



//    TableModel {
//            id: table_model

//        }

//    TableView {
//        id: table_view
//        anchors.fill: parent
//        bottomMargin: 100
//        topMargin: 60
//        leftMargin: 100
//        rightMargin: 100
////        rowSpacing: 1
////        columnSpacing: 1
////        columns: 2
////        rows:3
//        property var columnWidths: [100, 250, 250, 250, 250]
//        columnWidthProvider: function (column) { return columnWidths[column] }
//        rowHeightProvider: function (row) { return 80 }
//        model: table_model
//        delegate: Rectangle {
//                implicitWidth: 100
//                implicitHeight: 50
//                //border.width: 1
//                //border.color: "gray"
//                Text {
//                    text: display
//                    anchors.centerIn: parent
//                    font.pixelSize: Qt.application.font.pixelSize*1.2
//                    color: "black"
//                }
//                MouseArea {
//                    anchors.fill: parent
//                    hoverEnabled: true
//                    onEntered: {
//                        parent.color = "gray"
//                    }
//                    onExited: {
//                        parent.color = "white"
//                    }

//                    onClicked: {}
//                }
//                Rectangle {
//                    anchors.bottom: parent.bottom

//                    height: 1
//                    width: parent.width
//                    color: "black"

//                }

//            }

//    }
//   // }
//    function addRow(row_content) {
//        //table_model.addRow(['1','2'])
//        table_model.addHeader("序号")
//        table_model.addHeader("模型名称")
//        table_model.addRow(["1", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//        table_model.addRow(["2", "test"])
//    }
}

